from rest_framework import serializers
from django.core.validators import FileExtensionValidator,get_available_image_extensions

class UploadImageSerializer(serializers.Serializer):
    ## 校验上传的图片
    image = serializers.ImageField(
        validators=[FileExtensionValidator(['png', 'jpg', 'jpeg', 'gif'])],
        error_messages={'required':'请上传图片！','invalid_image':'请上传正确格式的图片'}
    )

    def validate_image(self,value):
        max_size = 0.5*1024*1024
        if value.size > max_size:
            raise serializers.ValidationError('图片最大不能超过0.5MB!')
        return value